package com.springcloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.springcloud.model.User;
import com.springcloud.service.ProviderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class ConsumerController {
    //注入Spring创建的声明式服务消费接口的代理对象
    @Resource
    private ProviderService providerService;

    @RequestMapping("/test")
    public Object test() {
        //访问远程服务
        String str = providerService.test();
        return "服务消费者----" + str;
    }

    @RequestMapping("/getUser01")
    public Object getUser01() {
        //访问远程服务
        String str = providerService.getUser01();
        return "服务消费者----" + str;
    }

    @RequestMapping("/getUser02")
    public Object getUser02() {
        //访问远程服务
        Map map = providerService.getUser02();
        return "服务消费者----" + map;
    }

    @RequestMapping("/getUser03")
    public Object getUser03() {
        //访问远程服务
        User user = providerService.getUser03();
        return "服务消费者----" + user;
    }

    @RequestMapping("/getUserList01")
    public Object getUserList01() {
        //访问远程服务
        List list = providerService.getUserList01();
        list.forEach(obj -> System.out.println(obj.getClass()));
        return "服务消费者----" + list;
    }

    @RequestMapping("/getUserList02")
    public Object getUserList02() {
        //访问远程服务
        List<User> list = providerService.getUserList02();
        list.forEach(obj -> System.out.println(obj.getClass()));
        return "服务消费者----" + list;
    }

    @RequestMapping("/params01")
    public Object params01() {
        //访问远程服务
        User user = providerService.params01("王五1", 25);

        return "服务消费者----" + user;
    }

    @RequestMapping("/params02")
    public Object params02() {
        //访问远程服务
        User user = providerService.params02("王五2", 25);
        return "服务消费者----" + user;
    }

    @RequestMapping("/paramsObj")
    public Object paramsObj() {
        User u = new User();
        u.setName("王五3");
        u.setAge(25);
        User user = providerService.paramsObj(u);
        return "服务消费者----" + user;
    }

    @RequestMapping("/paramsList")
    public Object paramsList() {
        List list = new ArrayList<User>();
        list.add(new User("王五4", 25));
        list.add(new User("王五5", 25));
        List list2 = providerService.paramsList(list);
        list2.forEach(obj -> System.out.println(obj.getClass()));
        return "服务消费者----" + list2;
    }
    @RequestMapping("/paramsJson")
    public Object paramsJson() {
        User u = new User();
        u.setName("王五6");
        u.setAge(25);
        User user=providerService.paramsJson(JSONObject.toJSONString(u));
        return "服务消费者----" + user;
    }
}
